package com.hspedu.newData.dmsxl.单调栈;

import org.junit.Test;

import java.util.Stack;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-26 
 * @Version: 1.0
 */

public class LC739每日温度 {


    // 暴力解法
    public int[] dailyTemperatures(int[] temperatures) {

        int[] res = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            int count = 0;
            for (int j = i+1; j < temperatures.length; j++) {
                count++;
                if (temperatures[j] > temperatures[i]){
                    res[i] = count;
                    break;
                }
            }
        }
        return res;
    }

    // 单调栈方式
    public int[] dailyTemperatures1(int[] temperatures) {

        Stack<Integer> stack = new Stack<>();

        int len = temperatures.length;
        int[] res = new int[len];

        for (int i = 0; i < len; i++) {
            while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
                Integer pop = stack.pop();
                res[pop] = i-pop;
            }
            stack.push(i);
        }

        return res;
    }


    @Test
    public void test() {
        // 测试代码
        int[] ints = dailyTemperatures1(new int[]{30,60,90});

        System.out.println("========");
    }


}
